package com.rc.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.rc.activity.R;
import com.rc.activity.dao.tbl_Category;
import com.rc.activity.dao.tbl_CategoryDao;
import com.rc.activity.dao.tbl_Count;
import com.rc.activity.dao.tbl_Item;
import com.rc.activity.dao.tbl_ItemDao;
import com.rc.activity.dao.tbl_Merchant;
import com.rc.activity.dao.tbl_MerchantDao;
import com.rc.activity.dao.tbl_Profile;
import com.rc.activity.dao.tbl_Receipt;
import com.rc.activity.dao.tbl_ReceiptDao;
import com.rc.activity.dao.tbl_Report;
import com.rc.activity.dao.tbl_ReportDao;
import com.rc.activity.dao.tbl_Trip;
import com.rc.activity.dao.tbl_TripDao;
import com.rc.activity.dao.tbl_TripItems;
import com.rc.activity.dao.tbl_TripItemsDao;
import com.rc.activity.dao.tbl_User;
import com.rc.activity.provider.Contract;
import com.rc.model.Model;
import com.rc.model.TaskType;
import com.rc.object.ReceiptObj;
import com.rc.object.TripObj;
import com.rc.util.Util;
import com.telpoo.frame.model.BaseModel;
import com.telpoo.frame.model.TaskParams;

public class DatabaseSupport {

	/**
	 * Lấy tbl_Category theo ID
	 */
	public static tbl_Category getCategoryByID(long mCategoryID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_CATEGORY,
				null,
				tbl_CategoryDao.Properties.CategoryID.columnName + " = '"
						+ mCategoryID + "'", null, null);
		ArrayList<tbl_Category> arrCategory = new ArrayList<tbl_Category>();
		arrCategory = tbl_Category.getObjectbyCursor(cursor);
		for (tbl_Category objCategory : arrCategory) {
			return objCategory;
		}
		return null;
	}

	public static long insertOrUpdateMerchant(tbl_Merchant objMerchant,
			Context ct) {
		long idMerchant = 0;
		Cursor cursor;
		cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_MERCHANT,
				null,
				tbl_MerchantDao.Properties.Address.columnName + " = '"
						+ objMerchant.getAddress() + "' and "
						+ tbl_MerchantDao.Properties.Name.columnName + " = '"
						+ objMerchant.getName().toString() + "'", null, null);
		if (cursor.getCount() > 0) {
			ArrayList<tbl_Merchant> arrMerchant = new ArrayList<tbl_Merchant>();
			arrMerchant = tbl_Merchant.getObjectbyCursor(cursor);
			for (tbl_Merchant _objMerchant : arrMerchant) {
				idMerchant = _objMerchant.getMerchantID();
			}
			objMerchant.setMerchantID(idMerchant);
			objMerchant.setIsUpdate("1");
			ct.getContentResolver().update(
					Contract.CONTENT_URI_MERCHANT,
					objMerchant.getContentValues(),
					tbl_MerchantDao.Properties.MerchantID.columnName + " = "
							+ objMerchant.getMerchantID(), null);
		} else {
			objMerchant.setIsCreate("1");
			Uri uri = ct.getContentResolver().insert(
					Contract.CONTENT_URI_MERCHANT,
					objMerchant.getContentValues());
			idMerchant = Long.valueOf(uri.getLastPathSegment());

		}
		return idMerchant;
	}

	/**
	 * Lấy tbl_Trip by ID
	 */
	public static tbl_Trip getTripByID(long mTripID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP,
				null,
				tbl_TripDao.Properties.TripID.columnName + " = '" + mTripID
						+ "'", null, null);
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		arrTrip = tbl_Trip.getObjectbyCursor(cursor);
		for (tbl_Trip objTrip : arrTrip) {
			return objTrip;
		}
		return null;
	}

	/**
	 * check report if draft report is true.
	 */
	public static boolean check_editReport(long mReportID, Context ct) {
		tbl_Report objReport = new tbl_Report();
		objReport = DatabaseSupport.getReportByID(mReportID, ct);
		if (objReport == null) {
			return false;
		}
		if (objReport.getReportType().equals(
				ct.getString(R.string.draft_reports))) {
			return true;
		}
		return false;
	}

	/**
	 * Check item if The item belong to trip which belong to report which is
	 * draft report
	 */
	public static boolean check_editItem(long mItemID, Context ct,
			BaseModel model) {
		tbl_Item objItem = new tbl_Item();
		objItem = getItemsByItemsID(mItemID, ct);
		if (objItem == null) {
			return true;
		}
		if (objItem.getTripID() == 0) {
			return true;
		}
		tbl_Trip objTrip = new tbl_Trip();
		objTrip = getTripByID(objItem.getTripID(), ct);
		if (objTrip == null) {
			String[] values = { objItem.getTripID() + "" };
			TaskParams params = new TaskParams(values);
			Model mModel = new Model(model, TaskType.TASK_GET_ITEM_FREE, null,
					ct);
			model.exeTask(params, mModel);
		}
		if (objTrip.getReport_ID() == 0) {
			return true;
		}
		return check_editReport(objTrip.getReport_ID(), ct);
	}

	/**
	 * Check trip if The trip belong to report which is draft report.
	 */
	public static boolean check_editTrip(long mTripID, Context ct) {
		tbl_Trip objTrip = new tbl_Trip();
		objTrip = getTripByID(mTripID, ct);
		if (objTrip == null) {
			return false;
		}
		tbl_User objUser = new tbl_User();
		objUser = getTbl_User(ct);
		if (objUser.getUserID() != Long.valueOf(objTrip.getUserID())) {
			return false;
		}
		tbl_Report objReport = new tbl_Report();
		if (objTrip.getReport_ID() == 0) {
			return true;
		}
		objReport = getReportByID(objTrip.getReport_ID(), ct);
		if (objReport == null) {
			return true;
		}
		if (objReport.getReportType().equals(
				ct.getString(R.string.draft_reports))) {
			return true;
		}

		return false;
	}

	/**
	 * Lấy tbl_Receipt by ID
	 */
	public static tbl_Receipt getReceiptByID(long mReceiptID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_RECEIPT,
				null,
				tbl_ReceiptDao.Properties.ReceiptID.columnName + " = '"
						+ mReceiptID + "'", null, null);
		ArrayList<tbl_Receipt> arrReceipt = new ArrayList<tbl_Receipt>();
		arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
		for (tbl_Receipt objReceipt : arrReceipt) {
			return objReceipt;
		}
		return null;
	}

	/**
	 * Lấy tbl_Receipt by MerchantID
	 */
	public static tbl_Receipt getReceiptByMerchantID(long mMerchant, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_RECEIPT,
				null,
				tbl_ReceiptDao.Properties.MerchantID.columnName + " = '"
						+ mMerchant + "'", null, null);
		ArrayList<tbl_Receipt> arrReceipt = new ArrayList<tbl_Receipt>();
		arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
		for (tbl_Receipt objReceipt : arrReceipt) {
			return objReceipt;
		}
		return null;
	}

	/**
	 * Lấy tbl_tripItem by ID
	 */
	public static tbl_TripItems getTripItemsByID(long mTripItemsID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP_ITEM,
				null,
				tbl_TripItemsDao.Properties.TripItemID.columnName + " = '"
						+ mTripItemsID + "'", null, null);
		ArrayList<tbl_TripItems> arrTripItems = new ArrayList<tbl_TripItems>();
		arrTripItems = tbl_TripItems.getObjectbyCursor(cursor);
		for (tbl_TripItems objTripItems : arrTripItems) {
			return objTripItems;
		}
		return null;
	}

	/**
	 * Lấy tbl_tripItem by TripID
	 */
	public static ArrayList<tbl_TripItems> getTripItemsByTripID(long TripID,
			Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP_ITEM,
				null,
				tbl_TripItemsDao.Properties.TripID.columnName + " = '" + TripID
						+ "'", null, null);
		ArrayList<tbl_TripItems> arrTripItems = new ArrayList<tbl_TripItems>();
		arrTripItems = tbl_TripItems.getObjectbyCursor(cursor);
		return arrTripItems;
	}

	/**
	 * Lấy tbl_Merchant by ID
	 */
	public static tbl_Merchant getMerchantByID(long mMerchantID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_MERCHANT,
				null,
				tbl_MerchantDao.Properties.MerchantID.columnName + " = '"
						+ mMerchantID + "'", null, null);
		ArrayList<tbl_Merchant> arrMerchant = new ArrayList<tbl_Merchant>();
		arrMerchant = tbl_Merchant.getObjectbyCursor(cursor);
		for (tbl_Merchant objMerchant : arrMerchant) {
			return objMerchant;
		}
		return null;
	}

	/**
	 * Lấy tbl_Report by ID
	 */
	public static tbl_Report getReportByID(long mReportID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_REPORT,
				null,
				tbl_ReportDao.Properties.ReportID.columnName + " = '"
						+ mReportID + "'", null, null);
		ArrayList<tbl_Report> arrReport = new ArrayList<tbl_Report>();
		arrReport = tbl_Report.getObjectbyCursor(cursor);
		for (tbl_Report objReport : arrReport) {
			return objReport;
		}
		return null;
	}

	/**
	 * get Report list by report type
	 */

	public static ArrayList<tbl_Report> getReportByReportType(int start,
			int end, String mReportType, Context ct) {
		String queryString = "";
		ArrayList<tbl_Report> arrReport = new ArrayList<tbl_Report>();
		String querybyType = tbl_ReportDao.Properties.ReportType.columnName
				+ " = '" + mReportType + "'";
		String queryDateString = tbl_ReportDao.Properties.FilterDate.columnName
				+ " >= " + start + " and "
				+ tbl_ReportDao.Properties.FilterDate.columnName + " <= " + end;
		if (start == 0 && end == 0) {
			queryDateString = "";
		}
		if (ct.getString(R.string.all_reports).equals(mReportType)) {
			queryString = "".equals(queryDateString) ? null : queryDateString;
		} else {
			queryString = "".equals(queryDateString) ? querybyType
					: querybyType + " and " + queryDateString;
		}
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_REPORT, null, queryString, null,
				tbl_ReportDao.Properties.FilterDate.columnName + " DESC ");
		arrReport = tbl_Report.getObjectbyCursor(cursor);

		return arrReport;
	}

	/**
	 * get Trip by Report ID
	 */

	public static ArrayList<tbl_Trip> getTripbyReportID(String mReportID,
			Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP,
				null,
				tbl_TripDao.Properties.Report_ID.columnName + " = '"
						+ mReportID + "'", null, null);
		return tbl_Trip.getObjectbyCursor(cursor);
	}

	/**
	 * get Items by receipt ID
	 */

	public static ArrayList<tbl_Item> getItemsByReceiptID(String mReceiptID,
			Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_ITEMS,
				null,
				tbl_ItemDao.Properties.ReceiptID.columnName + " = '"
						+ mReceiptID + "'", null, null);
		return tbl_Item.getObjectbyCursor(cursor);
	}

	/**
	 * get Items by receipt ID have categori different 0
	 */

	public static ArrayList<tbl_Item> getItemsByReceiptIDCategory(
			String mReceiptID, Context ct) {
		Cursor cursor = ct.getContentResolver()
				.query(Contract.CONTENT_URI_ITEMS,
						null,
						tbl_ItemDao.Properties.ReceiptID.columnName + " = '"
								+ mReceiptID + "' and "
								+ tbl_ItemDao.Properties.CategoryID.columnName
								+ " > 0", null, null);
		return tbl_Item.getObjectbyCursor(cursor);
	}

	/**
	 * get Items by item ID
	 */

	public static tbl_Item getItemsByItemsID(long mItemID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_ITEMS,
				null,
				tbl_ItemDao.Properties.ItemID.columnName + " = '" + mItemID
						+ "'", null, null);
		for (tbl_Item objItem : tbl_Item.getObjectbyCursor(cursor)) {
			return objItem;
		}
		return null;
	}

	/**
	 * get Items by Trip ID
	 */

	public static ArrayList<tbl_Item> getItemsByTripID(long mTripID, Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_ITEMS,
				null,
				tbl_ItemDao.Properties.TripID.columnName + " = '" + mTripID
						+ "'", null, null);
		return tbl_Item.getObjectbyCursor(cursor);
	}

	/**
	 * get Items by Receipt ID
	 */

	public static ArrayList<tbl_Item> getItemsByReceiptID(long mReceipt,
			Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_ITEMS,
				null,
				tbl_ItemDao.Properties.ReceiptID.columnName + " = '" + mReceipt
						+ "'", null, null);
		return tbl_Item.getObjectbyCursor(cursor);
	}

	/**
	 * get Items by Receipt ID, trip isn't in report
	 */

	public static ArrayList<tbl_Item> getItemsByReceiptIDNonReport(
			long mReceipt, Context ct) {
		ArrayList<tbl_Item> arrItem = new ArrayList<tbl_Item>();
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_ITEMS, null,
				tbl_ItemDao.Properties.ReceiptID.columnName + " = " + mReceipt,
				null, null);
		ArrayList<tbl_Item> arrItemNow = new ArrayList<tbl_Item>();
		arrItemNow = tbl_Item.getObjectbyCursor(cursor);
		if (arrItemNow != null) {
			for (int i = 0; i < arrItemNow.size(); i++) {
				tbl_Item objItem = new tbl_Item();
				objItem = arrItemNow.get(i);
				if (objItem.getTripID() == 0) {
					arrItem.add(objItem);
				}
			}
		}
		return arrItem;
	}

	/**
	 * Convert tbl_Receipt to ReceiptObj
	 */

	public static ReceiptObj convertReceiptObj(tbl_Receipt objReceipt,
			Context at) {

		if (objReceipt == null) {
			return null;
		}
		ReceiptObj ObjReceipt = new ReceiptObj();
		ObjReceipt.setReceiptID(objReceipt.getReceiptID());
		ObjReceipt.setMerchantID(objReceipt.getMerchantID());
		ObjReceipt.setDateTime(objReceipt.getPurchaseTime());
		ObjReceipt.setTotal(String.valueOf(objReceipt.getTotal()));
		ObjReceipt.setTax(String.valueOf(objReceipt.getTax()));
		ObjReceipt.setSubTotal(String.valueOf(objReceipt.getSubtotal()));
		ObjReceipt.setDiscount(String.valueOf(objReceipt.getDiscount()));
		ObjReceipt.setReceiptType(objReceipt.getReceiptType());
		ObjReceipt.setCurrencyCode(objReceipt.getCurrencyCode());
		ObjReceipt.setReceiptImage(objReceipt.getReceiptImage());
		ObjReceipt.setIsOpen(objReceipt.getIsOpened());
		String queryGetMerchantByID = tbl_MerchantDao.Properties.MerchantID.columnName
				+ " = '" + objReceipt.getMerchantID() + "'";
		Cursor cursor = at.getContentResolver().query(
				Contract.CONTENT_URI_MERCHANT, null, queryGetMerchantByID,
				null, null);
		if (cursor == null) {
			return null;
		}
		while (cursor.moveToNext()) {
			ObjReceipt
					.setMerchantName(cursor.getString(cursor
							.getColumnIndex(tbl_MerchantDao.Properties.Name.columnName)));
			ObjReceipt
					.setMerchantLogo(cursor.getString(cursor
							.getColumnIndex(tbl_MerchantDao.Properties.Logo.columnName)));
		}
		cursor.close();
		return ObjReceipt;
	}

	/**
	 * get ReceiptObj in SQLite
	 */
	public static ArrayList<ReceiptObj> getReceiptSQLite(int mReceiptType,
			long start, long end, Context at) {

		tbl_User objUser = new tbl_User();
		objUser = getTbl_User(at);
		String queryString = "";
		ArrayList<ReceiptObj> arrObjReceipt = new ArrayList<ReceiptObj>();
		String querybyType = tbl_ReceiptDao.Properties.ReceiptType.columnName
				+ " = " + mReceiptType;
		String queryDateString = tbl_ReceiptDao.Properties.CreatedTime.columnName
				+ " >= "
				+ start
				+ " and "
				+ tbl_ReportDao.Properties.CreatedTime.columnName
				+ " <= "
				+ end;
		if (start == 0 && end == 0) {
			queryDateString = "";
		}
		if (mReceiptType == 0) {
			queryString = "".equals(queryDateString) ? "" : queryDateString;
		} else {
			queryString = "".equals(queryDateString) ? querybyType
					: querybyType + " and " + queryDateString;
		}
		String queryLimit = tbl_ReceiptDao.Properties.CreatedTime.columnName
				+ " DESC ";
		// if (limitReceipt > 0) {
		// queryLimit = tbl_ReceiptDao.Properties.FilterDate.columnName
		// + " DESC LIMIT " + limitReceipt;
		// }
		String queryUser = tbl_ReceiptDao.Properties.UserID.columnName + " = "
				+ objUser.getUserID();
		queryString = "".equals(queryString) ? queryUser : queryString
				+ " and " + queryUser;
		Cursor cursor = at.getContentResolver().query(
				Contract.CONTENT_URI_RECEIPT, null, queryString, null,
				queryLimit);
		if (cursor == null) {
			return null;
		}
		ArrayList<tbl_Receipt> arrTbl_Receipt = new ArrayList<tbl_Receipt>();
		arrTbl_Receipt = tbl_Receipt.getObjectbyCursor(cursor);
		for (int i = 0; i < arrTbl_Receipt.size(); i++) {
			tbl_Receipt objtbl_Receipt = new tbl_Receipt();
			objtbl_Receipt = arrTbl_Receipt.get(i);

			arrObjReceipt.add(DatabaseSupport.convertReceiptObj(objtbl_Receipt,
					at));
		}

		return arrObjReceipt;
	}

	public static ArrayList<tbl_Receipt> getAllReceipt(Context ct) {
		return tbl_Receipt
				.getObjectbyCursor(ct.getContentResolver().query(
						Contract.CONTENT_URI_RECEIPT, null,
						tbl_ReceiptDao.Properties.IsDelete.columnName + "",
						null, null));
	}

	/**
	 * get ReceiptObj in SQLite
	 */
	public static ArrayList<tbl_Receipt> getReceiptAddExpense(int limitReceipt,
			int mReceiptType, long start, long end, Activity at) {

		String queryString = "";
		String querybyType = tbl_ReceiptDao.Properties.ReceiptType.columnName
				+ " = " + mReceiptType;
		String queryDateString = tbl_ReceiptDao.Properties.CreatedTime.columnName
				+ " >= "
				+ start
				+ " and "
				+ tbl_ReceiptDao.Properties.CreatedTime.columnName
				+ " <= "
				+ end;
		if (start == 0 && end == 0) {
			queryDateString = "";
		}
		if (mReceiptType == 0) {
			queryString = "".equals(queryDateString) ? "" : queryDateString;
		} else {
			queryString = "".equals(queryDateString) ? querybyType
					: querybyType + " and " + queryDateString;
		}
		queryString = queryString.equals("") ? tbl_ReceiptDao.Properties.Total.columnName
				+ " > 0"
				: queryString + " and "
						+ tbl_ReceiptDao.Properties.Total.columnName + " > 0";
		String queryLimit = null;
		if (limitReceipt > 0) {
			queryLimit = tbl_ReceiptDao.Properties.ReceiptID.columnName
					+ " LIMIT " + limitReceipt;
		}
		Cursor cursor = at.getContentResolver().query(
				Contract.CONTENT_URI_RECEIPT, null, queryString, null,
				queryLimit);
		if (cursor == null) {
			return null;
		}
		ArrayList<tbl_Receipt> arrTbl_Receipt = new ArrayList<tbl_Receipt>();
		arrTbl_Receipt = tbl_Receipt.getObjectbyCursor(cursor);

		return arrTbl_Receipt;
	}

	/**
	 * get tbl_User
	 */

	public static tbl_User getTbl_User(Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_USER, null, null, null, null);
		ArrayList<tbl_User> arrUser = new ArrayList<tbl_User>();
		arrUser = tbl_User.getObjectbyCursor(cursor);
		for (tbl_User objUser : arrUser) {
			return objUser;
		}
		return null;
	}

	/**
	 * get tbl_Profile
	 */

	public static tbl_Profile getTbl_Profile(Context ct) {
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_PROFILE, null, null, null, null);
		return tbl_Profile.getObjectbyCursor(cursor);

	}

	/**
	 * update item
	 */
	public static int updateItem(tbl_Item objItem, Context ct) {
		int row = 0;
		ArrayList<tbl_Item> arrItem = null;
		float amount = 0;
		// update item
		objItem.setIsUpdate("1");
		row = ct.getContentResolver().update(
				Contract.CONTENT_URI_ITEMS,
				objItem.getContentValues(),
				tbl_ItemDao.Properties.ItemID.columnName + " = "
						+ objItem.getItemID(), null);
		if (row > 0) {
			if (objItem.getTripID() > 0) {

				// get ArrayList item by TripID
				arrItem = new ArrayList<tbl_Item>();
				arrItem = getItemsByTripID(objItem.getTripID(), ct);

				for (tbl_Item _objItem : arrItem) {
					// Sum amount item for trip
					amount = amount + Util.convertFloat(_objItem.getAmount());
				}
				// update amount trip by ID
				updateTripByID(objItem.getTripID(), amount + "", ct);
			}

			// get objReceipt
			arrItem = new ArrayList<tbl_Item>();
			arrItem = getItemsByReceiptID(objItem.getReceiptID(), ct);
			amount = 0;
			for (tbl_Item _objItem : arrItem) {
				amount = amount + Util.convertFloat(_objItem.getAmount());
			}
			row = updateAmountReceipt(amount, objItem.getReceiptID(), ct);
			if (row == 1) {
				tbl_TripItems objTripItem = new tbl_TripItems();
				objTripItem = DatabaseSupport.getTripItemsByID(
						objItem.getItemID(), ct);
				if (objTripItem == null) {
					return row;
				}
				if (!objItem.getAmount().equals(objTripItem.getClaimed())) {
					objTripItem.setClaimed(Util.convertFloat(objItem
							.getAmount()));
					objTripItem.setIsUpdate("1");
				}
				row = ct.getContentResolver().update(
						Contract.CONTENT_URI_TRIP_ITEM,
						objTripItem.getContentValues(),
						tbl_TripItemsDao.Properties.TripItemID.columnName
								+ " = " + objTripItem.getTripItemID(), null);
				return row;
			}

		}

		return row;
	}

	public static boolean ruleRecipt(long mReceipt, Context ct) {
		ArrayList<tbl_Item> arrItem = new ArrayList<tbl_Item>();
		arrItem = getItemsByReceiptID(mReceipt, ct);
		for (tbl_Item objItem : arrItem) {
			if (!checkItemTrip(objItem.getItemID(), ct)) {
				return false;
			}
		}
		return true;
	}

	public static boolean checkItemTrip(long mItem, Context ct) {

		// get Item by ID
		tbl_Item objItem = new tbl_Item();
		objItem = getItemsByItemsID(mItem, ct);
		if (objItem == null) {
			return false;
		}

		// if tripID = 0.
		if (objItem.getTripID() == 0) {
			return true;
		}

		// get tbl_trip by ID
		tbl_Trip objTrip = new tbl_Trip();
		objTrip = getTripByID(objItem.getTripID(), ct);
		if (objTrip == null) {
			return true;
		}
		// if Report ID > 0, trip belong to report.
		if (objTrip.getReport_ID() > 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * update amount receipt
	 */
	public static int updateAmountReceipt(float amount, long mReceiptID,
			Context ct) {
		tbl_Receipt objReceipt = new tbl_Receipt();
		objReceipt = getReceiptByID(mReceiptID, ct);
		if (objReceipt == null) {
			return 0;
		}
		objReceipt.setSubtotal(Util.formatStringFloat(amount));
		float total = amount + Util.convertFloat(objReceipt.getTax())
				- Util.convertFloat(objReceipt.getDiscount());
		objReceipt.setTotal(Util.formatStringFloat(total));
		objReceipt.setIsUpdate("1");

		// update objReceipt
		return ct.getContentResolver().update(
				Contract.CONTENT_URI_RECEIPT,
				objReceipt.getContentValues(),
				tbl_ReceiptDao.Properties.ReceiptID.columnName + " = "
						+ objReceipt.getReceiptID(), null);
	}

	/**
	 * create new item
	 */
	public static int createNewItem(tbl_Item objItem, Context ct) {
		int row = 0;
		ArrayList<tbl_Item> arrItem = null;
		float amount = 0;
		// update item
		Uri uri = ct.getContentResolver().insert(Contract.CONTENT_URI_ITEMS,
				objItem.getContentValues());
		int id = Integer.valueOf(uri.getLastPathSegment());
		if (id > 0) {
			objItem.setItemID(Long.valueOf(id));
			if (objItem.getTripID() > 0) {

				// get ArrayList item by TripID
				arrItem = new ArrayList<tbl_Item>();
				arrItem = getItemsByTripID(objItem.getTripID(), ct);

				for (tbl_Item _objItem : arrItem) {
					// Sum amount item for trip
					amount = amount + Util.convertFloat(_objItem.getAmount());
				}
				// update amount trip by ID
				updateTripByID(objItem.getTripID(), amount + "", ct);
			}

			// get objReceipt
			arrItem = new ArrayList<tbl_Item>();
			arrItem = getItemsByReceiptID(objItem.getReceiptID(), ct);
			amount = 0;
			for (tbl_Item _objItem : arrItem) {
				amount = amount + Util.convertFloat(_objItem.getAmount());
			}
			row = updateAmountReceipt(amount, objItem.getReceiptID(), ct);

		}

		return row;
	}

	/**
	 * update amount trip by ID
	 */

	public static int updateTripByID(long mTripID, String amount, Context ct) {
		// get trip by ID
		int row = 0;
		tbl_Trip objTrip = new tbl_Trip();
		objTrip = getTripByID(mTripID, ct);
		if (objTrip == null) {
			return 0;
		}
		objTrip.setIsUpdate("1");
		objTrip.setAmount(amount + "");

		// update trip
		row = ct.getContentResolver().update(
				Contract.CONTENT_URI_TRIP,
				objTrip.getContentValues(),
				tbl_TripDao.Properties.TripID.columnName + " = "
						+ objTrip.getTripID(), null);
		if (row > 0) {

			// get object Report by Report ID
			tbl_Report objReport = new tbl_Report();
			objReport = getReportByID(objTrip.getReport_ID(), ct);
			if (objReport != null) {
				objReport.setAmount(objTrip.getAmount());
				objReport.setIsUpdate("1");

				// update amount report
				row = ct.getContentResolver().update(
						Contract.CONTENT_URI_REPORT,
						objReport.getContentValues(),
						tbl_ReportDao.Properties.ReportID.columnName + " = "
								+ objReport.getReportID(), null);
			}

		}
		return row;
	}

	/**
	 * update amount trip by ID
	 */

	public static int updateAmountCategorizeTripByID(long mTripID,
			String categorize, Context ct) {

		// get trip by ID
		int row = 0;
		tbl_Trip objTrip = new tbl_Trip();
		objTrip = getTripByID(mTripID, ct);
		if (objTrip == null) {
			return 0;
		}
		ArrayList<tbl_Item> arrItems = new ArrayList<tbl_Item>();
		arrItems = getItemsByTripID(mTripID, ct);
		float amount = 0;
		for (tbl_Item objItem : arrItems) {
			amount = amount + Util.convertFloat(objItem.getAmount());
		}
		objTrip.setCategories(categorize);
		objTrip.setIsUpdate("1");
		objTrip.setAmount(amount + "");

		// update trip
		row = ct.getContentResolver().update(
				Contract.CONTENT_URI_TRIP,
				objTrip.getContentValues(),
				tbl_TripDao.Properties.TripID.columnName + " = "
						+ objTrip.getTripID(), null);
		if (row > 0) {

			// get object Report by Report ID
			tbl_Report objReport = new tbl_Report();
			objReport = getReportByID(objTrip.getReport_ID(), ct);
			if (objReport != null) {
				objReport.setAmount(objTrip.getAmount());
				objReport.setIsUpdate("1");

				// update amount report
				row = ct.getContentResolver().update(
						Contract.CONTENT_URI_REPORT,
						objReport.getContentValues(),
						tbl_ReportDao.Properties.ReportID.columnName + " = "
								+ objReport.getReportID(), null);
			}

		}
		return row;
	}

	/**
	 * delete item receipt
	 */
	public static int multiUpdate(long mReceiptID,
			ArrayList<ContentValues> arrContent, Context ct) {
		int row = 0;
		if (arrContent == null) {
			return 0;
		}
		float amount = 0;
		for (ContentValues values : arrContent) {
			if ("1".equals(values
					.get(tbl_ReceiptDao.Properties.IsCreate.columnName))) {
				row = row
						+ ct.getContentResolver().delete(
								Contract.CONTENT_URI_ITEMS,
								tbl_ItemDao.Properties.ItemID.columnName, null);
			} else {
				row = row
						+ ct.getContentResolver()
								.update(Contract.CONTENT_URI_ITEMS,
										values,
										tbl_ItemDao.Properties.ItemID.columnName
												+ " = "
												+ values.getAsString(tbl_ItemDao.Properties.ItemID.columnName),
										null);
			}

			long mTripID = 0;
			mTripID = values
					.getAsLong(tbl_ItemDao.Properties.TripID.columnName);
			if (mTripID != 0) {
				ArrayList<tbl_Item> arrItems = new ArrayList<tbl_Item>();
				arrItems = getItemsByTripID(mTripID, ct);

				if (arrItems == null) {
					return 0;
				}
				for (tbl_Item objItem : arrItems) {
					amount = amount + Util.convertFloat(objItem.getAmount());
				}

				if (row > 0) {
					row = updateTripByID(mTripID, amount + "", ct);
				}
			}

		}
		updateAmountReceipt(amount, mReceiptID, ct);
		return row;
	}

	/**
	 * Get data Report Type
	 */

	public static ArrayList<tbl_Count> getArrReportTypes(Resources resousce,
			Context ct) {
		String[] arrReceiptTypeText = resousce
				.getStringArray(R.array.filter_report);
		ArrayList<tbl_Count> arrReportType = new ArrayList<tbl_Count>();
		for (String string : arrReceiptTypeText) {
			tbl_Count objCount = new tbl_Count();
			objCount.setName(string);
			String textSQL = tbl_ReportDao.Properties.ReportType.columnName
					+ " = '" + string + "'";
			if (ct.getString(R.string.all_reports).equals(string)) {
				textSQL = null;
			}
			Cursor cursor = ct.getContentResolver().query(
					Contract.CONTENT_URI_REPORT, null, textSQL, null, null);

			objCount.setTotal("" + cursor.getCount());
			arrReportType.add(objCount);
			cursor.close();
		}
		return arrReportType;
	}

	/**
	 * Get data Receipt Type
	 */

	public static ArrayList<tbl_Count> getArrReceiptTypes(Resources resousce,
			Context ct) {
		String[] arrReceiptTypeText = resousce
				.getStringArray(R.array.filter_receipt);
		ArrayList<tbl_Count> arrReceiptType = new ArrayList<tbl_Count>();
		for (String string : arrReceiptTypeText) {
			tbl_Count objCount = new tbl_Count();
			objCount.setName(string);
			if (string.equals(ct.getString(R.string.all_receipts))) {
				string = "0";
			} else if (string.equals(ct.getString(R.string.paper_receipts))) {
				string = "3";
			} else if (string.equals(ct.getString(R.string.email_receipts))) {
				string = "2";
			} else if (string.equals(ct.getString(R.string.manual_receipts))) {
				string = "4";
			} else if (string.equals(ct.getString(R.string.paper_invoice))) {
				string = "6";
			}
			String textSQL = tbl_ReceiptDao.Properties.ReceiptType.columnName
					+ " = '" + string + "'";
			if ("0".equals(string)) {
				textSQL = null;
			}
			Cursor cursor = ct.getContentResolver().query(
					Contract.CONTENT_URI_RECEIPT, null, textSQL, null, null);

			objCount.setTotal("" + cursor.getCount());
			arrReceiptType.add(objCount);
			cursor.close();
		}
		return arrReceiptType;
	}

	/**
	 * Get data Trip Type
	 */

	public static ArrayList<tbl_Count> getArrTripTypes(Resources resousce,
			Context ct) {
		tbl_User objUser = new tbl_User();
		objUser = DatabaseSupport.getTbl_User(ct);
		if (objUser == null) {
			return null;
		}
		String[] arrReceiptTypeText = resousce
				.getStringArray(R.array.filter_trip);
		ArrayList<tbl_Count> arrTripType = new ArrayList<tbl_Count>();
		for (String string : arrReceiptTypeText) {
			tbl_Count objCount = new tbl_Count();
			objCount.setName(string);
			String textSQL = tbl_TripDao.Properties.UserID.columnName + " = '"
					+ objUser.getUserID() + "' and "
					+ tbl_TripDao.Properties.Type.columnName + " = '" + string
					+ "'";
			if (ct.getString(R.string.all_trips).equals(string)) {
				textSQL = tbl_TripDao.Properties.UserID.columnName + " = '"
						+ objUser.getUserID() + "'";
			}
			Cursor cursor = ct.getContentResolver().query(
					Contract.CONTENT_URI_TRIP, null, textSQL, null, null);

			objCount.setTotal("" + cursor.getCount());
			arrTripType.add(objCount);
			cursor.close();
		}
		return arrTripType;
	}

	/**
	 * validate datetime
	 */

	public static boolean checkStartDateEndDate(String endDate,
			String startDate, Context ct) {
		int endInt = Util.parseDateForInt(endDate);
		// for (tbl_Trip objTrip : arrTrip) {
		// return false;
		// }
		int startInt = Util.parseDateForInt(startDate);
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP,
				null,
				startInt + " < "
						+ tbl_TripDao.Properties.StartDateInt.columnName
						+ " and " + endInt + " > "
						+ tbl_TripDao.Properties.EndDateInt.columnName, null,
				null);
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		arrTrip = tbl_Trip.getObjectbyCursor(cursor);
		if (arrTrip.size() > 0) {
			return false;
		}

		return true;
	}

	/**
	 * Kiểm tra biến reference trong database nếu đã tồn tại thì + 1 theo mã
	 * hexadecimal
	 */
	@SuppressLint("DefaultLocale")
	public static String validateReference(String reference, Context ct) {

		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP,
				null,
				tbl_TripDao.Properties.Reference.columnName + " like '"
						+ reference + "%' ", null, null);
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		arrTrip = tbl_Trip.getObjectbyCursor(cursor);
		if (arrTrip.size() > 0) {
			reference = arrTrip.get((arrTrip.size() - 1)).getReference();
			String key = reference.substring(reference.length() - 1,
					reference.length());
			try {
				Integer.parseInt(key);
				int i = 11;
				reference = (reference + Integer.toHexString(i)).toUpperCase();
			} catch (NumberFormatException e) {
				int decimal = Integer.parseInt(key, 16);
				decimal = decimal + 1;
				reference = reference.substring(0, (reference.length() - 1));
				reference = (reference + Integer.toHexString(decimal))
						.toUpperCase();

			}
		}
		return reference;
	}

	/**
	 * check sqlite startdate
	 */

	public static String checkStartDate(String startDate, Context ct) {
		int startInt = Util.parseDateForInt(startDate);
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP,
				null,
				startInt + " <= "
						+ tbl_TripDao.Properties.EndDateInt.columnName
						+ " and " + startInt + " >= "
						+ tbl_TripDao.Properties.StartDateInt.columnName, null,
				null);
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		arrTrip = tbl_Trip.getObjectbyCursor(cursor);
		if (arrTrip.size() > 0) {
			startDate = arrTrip.get(0).getEndDate();
			int startDateInt = arrTrip.get(0).getStartDateInt();
			for (tbl_Trip objTrip : arrTrip) {
				if (startDateInt > objTrip.getStartDateInt()) {
					startDate = objTrip.getEndDate();
					startDateInt = objTrip.getStartDateInt();
				}
			}
		}

		return startDate;
	}

	/**
	 * check sqlite startdate
	 */

	public static String checkEndDate(String endDate, String startDate,
			Context ct) {
		int endInt = Util.parseDateForInt(endDate);
		int startInt = Util.parseDateForInt(startDate);
		String query = "";
		if (endInt == startInt) {
			query = startInt + " < "
					+ tbl_TripDao.Properties.EndDateInt.columnName + " and "
					+ startInt + " > "
					+ tbl_TripDao.Properties.StartDateInt.columnName;
		} else {
			query = "( " + startInt + " >= "
					+ tbl_TripDao.Properties.StartDateInt.columnName + " and "
					+ startInt + " < "
					+ tbl_TripDao.Properties.EndDateInt.columnName + " and "
					+ tbl_TripDao.Properties.StartDateInt.columnName + " != "
					+ tbl_TripDao.Properties.EndDateInt.columnName + ") or ("
					+ startInt + " < "
					+ tbl_TripDao.Properties.StartDateInt.columnName + " and "
					+ tbl_TripDao.Properties.StartDateInt.columnName + " < "
					+ endInt + " )";
		}
		Log.e("SQL-query", query);
		tbl_User objUser = new tbl_User();
		objUser = getTbl_User(ct);
		if (objUser == null) {
			return "";
		}

		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP,
				null,
				query + " and " + tbl_TripDao.Properties.UserID.columnName
						+ " = " + objUser.getUserID(), null, null);
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		arrTrip = tbl_Trip.getObjectbyCursor(cursor);
		if (arrTrip != null) {
			if (arrTrip.size() != 0) {
				int min = 0;
				min = arrTrip.get(0).getStartDateInt();
				endDate = arrTrip.get(0).getStartDate();
				for (int i = 0; i < arrTrip.size(); i++) {
					if (min > arrTrip.get(i).getStartDateInt()) {
						min = arrTrip.get(i).getStartDateInt();
						endDate = arrTrip.get(i).getStartDate();
					}
				}
			}
		}

		return endDate;
	}

	/**
	 * add trip to report
	 */
	public static void addTripToReport(long reportID, long tripID, Context ct) {
		if (tripID == 0 && reportID == 0) {
			return;
		}

		tbl_Trip objTrip = new tbl_Trip();
		objTrip = getTripByID(tripID, ct);
		if (objTrip == null) {
			return;
		}
		objTrip.setReport_ID(reportID);
		objTrip.setIsUpdate("1");
		if (ct.getContentResolver()
				.update(Contract.CONTENT_URI_TRIP,
						objTrip.getContentValues(),
						tbl_TripDao.Properties.TripID.columnName + " = "
								+ tripID, null) != -1) {
			tbl_Report objReport = new tbl_Report();
			objReport = getReportByID(reportID, ct);
			if (objReport == null) {
				return;
			}
			objReport.setAmount(objTrip.getAmount() + "");
			objReport.setClaimed(objTrip.getAmount() + "");
			objReport.setIsUpdate("1");
			if (ct.getContentResolver().update(
					Contract.CONTENT_URI_REPORT,
					objReport.getContentValues(),
					tbl_ReportDao.Properties.ReportID.columnName + " = "
							+ reportID, null) != -1) {
				ArrayList<tbl_Item> arrItems = new ArrayList<tbl_Item>();
				arrItems = getItemsByTripID(tripID, ct);
				for (tbl_Item objItem : arrItems) {
					tbl_TripItems objTripItem = new tbl_TripItems();
					float approved = 0;
					objTripItem.setApproved(approved);
					objTripItem.setClaimed(Util.convertFloat(objItem
							.getAmount()));
					objTripItem.setIsApproved(0);
					objTripItem.setIsClaimed(1);
					objTripItem.setIsCreate("1");
					objTripItem.setIsDelete("");
					objTripItem.setTripID(tripID + "");
					objTripItem.setTripItemID(objItem.getItemID());
					if (ct.getContentResolver().update(
							Contract.CONTENT_URI_TRIP_ITEM,
							objTripItem.getContentValues(),
							tbl_TripItemsDao.Properties.TripItemID.columnName
									+ " = " + objItem.getItemID(), null) != -1) {
						break;
					}
				}
			}
		}

	}

	/**
	 * Kiểm tra biến reference trong database nếu đã tồn tại thì + 1 theo mã
	 * hexadecimal
	 */
	public static String validateReferenceReport(String reference, Context ct) {

		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_REPORT,
				null,
				tbl_ReportDao.Properties.Reference.columnName + " like '"
						+ reference + "%' ", null, null);
		ArrayList<tbl_Report> arrReport = new ArrayList<tbl_Report>();
		arrReport = tbl_Report.getObjectbyCursor(cursor);
		if (arrReport.size() > 0) {
			reference = arrReport.get((arrReport.size() - 1)).getReference();
			String key = reference.substring(reference.length() - 1,
					reference.length());
			try {
				Integer.parseInt(key);
				int i = 11;
				reference = (reference + Integer.toHexString(i)).toUpperCase();
			} catch (NumberFormatException e) {
				int decimal = Integer.parseInt(key, 16);
				decimal = decimal + 1;
				reference = reference.substring(0, (reference.length() - 1));
				reference = (reference + Integer.toHexString(decimal))
						.toUpperCase();
			}
		}
		return reference;
	}

	public static int addTripToReport(long tripID, Context ct) {
		tbl_Report objReport = new tbl_Report();
		tbl_Trip objTrip = new TripObj();
		objTrip = getTripByID(tripID, ct);
		if (objTrip == null) {
			return 0;
		}
		objReport.setIsCreate("1");
		objReport.setIsDelete("");
		objReport.setAmount(objTrip.getAmount());
		objReport.setTitle(objTrip.getName());
		objReport.setIsSubmitted(0);
		objReport.setClaimed(objTrip.getAmount());
		objReport.setDate(Util.getDefaultInfor());
		objReport.setCreatedTime(Util.getDefaultInfor());
		objReport.setReportID(Long.valueOf(Util.getCurrentTime()));
		objReport.setIsArchived(0);
		objReport.setApproverEmail("");
		objReport.setReportType(ct.getString(R.string.draft_reports));
		SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd",
				Locale.getDefault());
		String reference = "R" + dft.format(Calendar.getInstance().getTime());
		objReport.setReference(reference);
		objReport.setFilterDate(Integer.valueOf(dft.format(Calendar
				.getInstance().getTime())));
		objReport.setBug("0");
		objReport.setIsApproved(0);
		Uri uri = ct.getContentResolver().insert(Contract.CONTENT_URI_REPORT,
				objReport.getContentValues());
		if (uri != null) {
			int reportID = Integer.valueOf(uri.getLastPathSegment());
			if (reportID != -1) {
				objTrip.setReport_ID(Long.valueOf(reportID));
				ct.getContentResolver().update(
						Contract.CONTENT_URI_TRIP,
						objTrip.getContentValues(),
						tbl_TripDao.Properties.TripID.columnName + " = "
								+ objTrip.getTripID(), null);
			}
			return reportID;
		}
		return 0;
	}

	public static ArrayList<tbl_Trip> getTripsPending(Context ct) {
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		ArrayList<tbl_Report> arrReports = new ArrayList<tbl_Report>();

		// get report peding
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_REPORT,
				null,
				tbl_ReportDao.Properties.ReportType.columnName + " = '"
						+ ct.getString(R.string.pending_reports) + "'", null,
				null);
		arrReports = tbl_Report.getObjectbyCursor(cursor);
		if (arrReports != null) {
			if (arrReports.size() > 0) {
				for (tbl_Report objReport : arrReports) {
					arrTrip = DatabaseSupport.getTripbyReportID(
							objReport.getReportID() + "", ct);
				}
			} else {
				return null;
			}
		} else {
			return null;
		}
		return arrTrip;
	}

	public static int updateDataClaim(tbl_Item objItem, boolean isClaim,
			Context ct) {
		tbl_TripItems objTripItem = new tbl_TripItems();
		objTripItem = DatabaseSupport.getTripItemsByID(objItem.getItemID(), ct);
		objTripItem.setIsUpdate("1");
		int row = -1;
		if (objTripItem != null) {
			if (isClaim) {
				objTripItem.setIsClaimed(1);
			} else {
				objTripItem.setIsClaimed(0);
			}

			// Update tbl_TripItem
			row = ct.getContentResolver().update(
					Contract.CONTENT_URI_TRIP_ITEM,
					objTripItem.getContentValues(),
					tbl_TripItemsDao.Properties.TripItemID.columnName + " = '"
							+ objTripItem.getTripItemID() + "'", null);

		}
		return row;

	}

	public static void updateDataReject(tbl_Item objItem, boolean isReject,
			Context ct) {
		tbl_TripItems objTripItem = new tbl_TripItems();
		objTripItem = DatabaseSupport.getTripItemsByID(objItem.getItemID(), ct);
		objTripItem.setIsUpdate("1");
		if (objTripItem != null) {
			if (isReject) {
				objTripItem.setIsApproved(1);
			} else {
				objTripItem.setIsApproved(0);
			}

			// Update tbl_TripItem
			ct.getContentResolver().update(
					Contract.CONTENT_URI_TRIP_ITEM,
					objTripItem.getContentValues(),
					tbl_TripItemsDao.Properties.TripItemID.columnName + " = '"
							+ objTripItem.getTripItemID() + "'", null);

		}

	}

	/**
	 * get trip list by trip type
	 */

	public static ArrayList<tbl_Trip> getTripByTripType(int start, int end,
			String mTripType, Context ct) {
		String queryString = "";
		tbl_User objUser = new tbl_User();
		objUser = DatabaseSupport.getTbl_User(ct);
		String queryDateString = tbl_TripDao.Properties.FilterDate.columnName
				+ " >= " + start + " and "
				+ tbl_TripDao.Properties.FilterDate.columnName + " <= " + end;
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		String querybyType = tbl_TripDao.Properties.Type.columnName + " = '"
				+ mTripType + "'";
		if (start == 0 && end == 0) {
			queryDateString = "";
		}
		if (ct.getString(R.string.all_trips).equals(mTripType)) {
			queryString = "".equals(queryDateString) ? tbl_TripDao.Properties.UserID.columnName
					+ " = '" + objUser.getUserID() + "'"
					: tbl_TripDao.Properties.UserID.columnName + " = '"
							+ objUser.getUserID() + "' and " + queryDateString;
		} else {
			queryString = "".equals(queryDateString) ? tbl_TripDao.Properties.UserID.columnName
					+ " = '" + objUser.getUserID() + "' and " + querybyType
					: tbl_TripDao.Properties.UserID.columnName + " = '"
							+ objUser.getUserID() + "' and " + querybyType
							+ " and " + queryDateString;
		}
		Cursor cursor = ct.getContentResolver().query(
				Contract.CONTENT_URI_TRIP, null, queryString, null,
				tbl_TripDao.Properties.StartDateInt.columnName + " DESC ");
		arrTrip = tbl_Trip.getObjectbyCursor(cursor);

		return arrTrip;
	}

	/**
	 * update categoryID for items
	 */
	public static int updateItemsCategoryID(ArrayList<String> arrItems, long mCategoryID,
			Context ct, long mTripID) {
		int row = 0;

		if (arrItems.size() > 0) {
			for (String mItemsID : arrItems) {
				tbl_Item objItem = new tbl_Item();
				objItem = DatabaseSupport.getItemsByItemsID(
						Long.valueOf(mItemsID), ct);
				if (objItem != null) {
					objItem.setTripID(mTripID);
					objItem.setCategoryID(mCategoryID);
					objItem.setIsUpdate("1");
					row = row
							+ ct.getContentResolver()
									.update(Contract.CONTENT_URI_ITEMS,
											objItem.getContentValues(),
											tbl_ItemDao.Properties.ItemID.columnName
													+ " = "
													+ objItem.getItemID(), null);
					tbl_TripItems objTripItem = new tbl_TripItems();
					objTripItem.setTripID(mTripID + "");
					float appreoved = 0;
					objTripItem.setApproved(appreoved);
					objTripItem.setClaimed(Float.valueOf(objItem.getAmount()));
					objTripItem.setIsCreate("1");
					objTripItem.setIsClaimed(1);
					objTripItem.setIsDelete("");
					objTripItem.setTripItemID(objItem.getItemID());
					objTripItem.setIsApproved(0);
					if (ct.getContentResolver().insert(
							Contract.CONTENT_URI_TRIP_ITEM,
							objTripItem.getContentValues()) != null) {
						tbl_Trip objTrip = new tbl_Trip();
						objTrip = DatabaseSupport.getTripByID(mTripID, ct);
						if (objTrip != null) {
							String categories = "";
							categories = objTrip.getCategories().indexOf(
									mCategoryID + "") == -1 ? objTrip
									.getCategories() + "," + mCategoryID
									: categories;
							if (!"".equals(categories)) {
								objTrip.setCategories(categories);
								ct.getContentResolver()
										.update(Contract.CONTENT_URI_TRIP,
												objTrip.getContentValues(),
												tbl_TripDao.Properties.TripID.columnName
														+ " = "
														+ objTrip.getTripID(),
												null);
							}

						}
					}

				}
			}
		}

		return row;
	}
}
